# Maintainer: Lucio Perusini <lucioperusini@gmail.com>
# Contributor: R4SAS <r4sas@i2pmail.org>

_realname=i2pd
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=2.47.0
pkgrel=3
pkgdesc='A full-featured C++ implementation of the I2P router (mingw-w64)'
url='https://i2pd.website/'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-miniupnpc"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/PurpleI2P/i2pd/archive/${pkgver}.tar.gz"
        "0001-i2pd-makefile.patch"
        "0002-cmake-nonstatic.patch::https://github.com/PurpleI2P/i2pd/commit/8677cd54bd4d145e3bba7044484dcba4eed73a85.patch")
sha256sums=('c988baf23215c37d5f566b7b2059a3c168c78d157eac6dc04a30ac266c6335f0'
            '4f0927a0ad735cd0fa30f5ef57f4ac686c07d256ac7f015722a65b0c1b2a0b8f'
            'ebb8fe70f750da31243db0b907b9aaec08c4463d622b9088de045ef93285c79e')
noextract=("${_realname}-${pkgver}.tar.gz") # symlinks

prepare() {
  tar -xf "${_realname}-${pkgver}.tar.gz" || true
  cd "${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}"/0001-i2pd-makefile.patch
  patch -Np1 -i "${srcdir}"/0002-cmake-nonstatic.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    "${_extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DWITH_UPNP=ON \
    ../${_realname}-${pkgver}/build

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  # bin
  install -d "${pkgdir}${MINGW_PREFIX}/bin/"
  cp i2pd.exe "${pkgdir}${MINGW_PREFIX}/bin/"

  # config
  install -d "${pkgdir}${MINGW_PREFIX}/etc/i2pd"
  cp ../${_realname}-${pkgver}/contrib/{i2pd,tunnels}.conf "${pkgdir}${MINGW_PREFIX}/etc/i2pd"
  install -d "${pkgdir}${MINGW_PREFIX}/etc/i2pd/tunnels.d"
  cp ../${_realname}-${pkgver}/contrib/tunnels.d/{*.conf,README} "${pkgdir}${MINGW_PREFIX}/etc/i2pd/tunnels.d"

  # certificates
  install -d "${pkgdir}${MINGW_PREFIX}/share/i2pd"
  cp -r ../${_realname}-${pkgver}/contrib/certificates "${pkgdir}${MINGW_PREFIX}/share/i2pd"

  # license
  install -Dm644 ../${_realname}-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
}
